package io.renren.modules.sys.util;

import io.renren.modules.sys.util.DateUtil;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.net.URL;
import java.net.URLConnection;
import java.util.TimeZone;

/**
 * Created by dell on 2019/12/16.
 */
public class Verification {
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
    public static String retruedata(String code) {
        String cod=code.substring(0,code.length()-16);
        try {
            String DeString = Decrypt(cod,code.substring(code.length()-16,code.length()));
            int codesty = DeString.substring(DeString.indexOf("MACHINECODE="), DeString.indexOf("hxbklxhisnotnaver")).indexOf(Creatcoed.getMachineCode());
            if (codesty > 0) {
                int codes = DeString.substring(DeString.indexOf("CENSETYPE="), DeString.indexOf("hxbklxhtime")).indexOf("yes");
                if (codes > 0) {
                    return "1";
                } else {
                    String codetime = DeString.substring(DeString.indexOf("time=") + 5, DeString.indexOf("hxbklxhMACHINECODE"));
                    TimeZone.setDefault(TimeZone.getTimeZone("GMT+8")); // 时区设置
                    URL url = new URL("http://time1909.beijing-time.org/time.asp");//取得资源对象
                    URLConnection uc = url.openConnection();//生成连接对象
                    uc.connect(); //发出连接
                    //分别取得时间中的小时，分钟和秒，并输出
                    if (uc.getDate() - DateUtil.formatString(codetime,"yyyy-mm-dd").getTime() < 0) {
                        return "已授权，到期时间："+codetime;
                    }else {
                        return "已到期，到期时间："+codetime;
                    }
                }
            } else {
                return "0";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "0";
        }

    }

}
